package com.jnevision.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.jnevision.config.interceptors.LoginInterceptor;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("PUT", "DELETE", "GET", "POST")
            .allowedHeaders("*").allowCredentials(true).maxAge(3600);
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
            .maxAge(3600).allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login",
            "/sysconfigs/baidubce", "/appversons/current", "/index.html", "/notices/**", "/swagger-ui.html",
            "/salarys/export", "/webjars/**","/sysconfigs/aboutus","/aboutus.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/", "classpath:/static/view/",
            "classpath:/static/assets/", "classpath:/public/", "classpath:/****/", "classpath:/static/",
            "classpath:/templates/");
    }
}
